﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.IO;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Xpom_Xpum__Integrated_Development_Environment.Data.Configuration;
using Xpom_Xpum__Integrated_Development_Environment.View;
using Xpom_Xpum__Integrated_Development_Environment.ViewModel;

namespace Xpom_Xpum__Integrated_Development_Environment
{
    /// <summary>
    /// Interaction logic for MainWindowView.xaml
    /// </summary>
    public partial class MainWindowView : Window, System.Windows.Forms.IWin32Window
    {
        #region Constructor

        public MainWindowView()
        {
            InitializeComponent();

            if (Properties.Settings.Default.MainWindowIsMaximized)
                this.WindowState = System.Windows.WindowState.Maximized;

            ((App)App.Current).pev = pev;
        }

        #endregion

        #region Commands

        #endregion

        #region Fields

        #endregion

        #region Properties

        public IntPtr Handle
        {
            get
            {
                var interopHelper = new WindowInteropHelper(this);
                return interopHelper.Handle;
            }
        }

        #endregion

        #region Methods

        protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
        {
            Properties.Settings.Default.Save();
            base.OnClosing(e);
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            App.Current.Shutdown();
        }

        private void TabControlView_MouseDown(object sender, MouseButtonEventArgs e)
        {
        }

        private void Image_ImageFailed(object sender, ExceptionRoutedEventArgs e)
        {
        }

        private void Window_StateChanged(object sender, EventArgs e)
        {
            if (this.WindowState != System.Windows.WindowState.Minimized)
            {
                Properties.Settings.Default.MainWindowIsMaximized =
                    (this.WindowState == System.Windows.WindowState.Maximized);
            }
        }

        /// <summary>
        /// New
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            ((App)App.Current).mvm.FileNewClickMethod();
        }

        /// <summary>
        /// Open
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            ((App)App.Current).mvm.FileOpenClickMethod();
        }

        /// <summary>
        /// Save
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            ((App)App.Current).tcvwm.SaveCurrent();
        }

        /// <summary>
        /// Save All
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click_3(object sender, RoutedEventArgs e)
        {
            ((App)App.Current).tcvwm.SaveAll();
        }

        /// <summary>
        /// Cut
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click_4(object sender, RoutedEventArgs e)
        {
            ((App)App.Current).tcvwm.Cut();
        }

        /// <summary>
        /// Copy
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click_5(object sender, RoutedEventArgs e)
        {
            ((App)App.Current).tcvwm.Copy();
        }

        /// <summary>
        /// Paste
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click_6(object sender, RoutedEventArgs e)
        {
            ((App)App.Current).tcvwm.Paste();
        }

        /// <summary>
        /// Undo
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click_7(object sender, RoutedEventArgs e)
        {
            ((App)App.Current).tcvwm.Undo();
        }

        /// <summary>
        /// Redo
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click_8(object sender, RoutedEventArgs e)
        {
            ((App)App.Current).tcvwm.Redo();
        }

        /// <summary>
        /// Run With No Debug
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click_9(object sender, RoutedEventArgs e)
        {
            if (((App)App.Current).mvm.IsProjectOpened)
            {
                string programFiles = Environment.GetEnvironmentVariable("ProgramFiles");
                string programFilesx64 = Environment.GetEnvironmentVariable("ProgramFiles(x86)");

                string browserExePath = ((App)App.Current).iswvm.Browsers[((App)App.Current).iswvm.SelectedBrowserIndex].BrowserExecutablePath;
                string browserExePathFinal = browserExePath.Replace("%ProgramFiles%",
                programFiles);
                if (!File.Exists(browserExePathFinal))
                {
                    browserExePathFinal = browserExePath.Replace("%ProgramFiles%",
                    programFilesx64);
                }

                var CRXProcess = new Process();
                CRXProcess.StartInfo.Arguments =
                    "-run \"" + browserExePathFinal.Replace("\\", "/") +
                    "\" \"" + ((App)App.Current).pevm.ProjectDirectory.Replace("\\", "/");
                //ZIPProcess.StartInfo.CreateNoWindow = true;
                CRXProcess.StartInfo.FileName = GlobalSettings.SDKPath + "Environment/Xpom-Xpum! Browser Integrator/xbint.exe";
                CRXProcess.StartInfo.UseShellExecute = false;
                CRXProcess.Start();
            }
        }

        private void Button_Click_10(object sender, RoutedEventArgs e)
        {

        }

        #endregion

    }
}
